/*
 * =====================================================================================
 *
 *       Filename:  file_system.c
 *
 *    Description:  
 *
 *        Version:  1.0
 *        Created:  07/27/2011 01:54:20 PM
 *       Revision:  none
 *       Compiler: gcc
 *
 *         Author:  Bran
 *        Company:  
 *
 * =====================================================================================
 */

#include <stdio.h>
#include <sys/vfs.h>
#include <stdlib.h>
#include <string.h>
#include <assert.h>

long long free_space(char *pPath, char *pType)
{
    assert(pPath != NULL);
    struct statfs fs;
    long long size = 0;
    char type;

    if (pType == NULL)
    {
        type = 'b';
    }else{
        type = *pType;
    }

    if (statfs(pPath, &fs))
    {
        return -1;
    }

    switch(type)
    {
        case 'b':
            size = fs.f_bsize * fs.f_bfree;
            break;
        case 'k':
            size = fs.f_bsize * fs.f_bfree / 1024;
            break;
        case 'm':
            size = fs.f_bsize * fs.f_bfree / (1024 * 1024);
            break;
        default:
            size = fs.f_bsize * fs.f_bfree;
    }

    return size;
}

#define LINE_SIZE 1024
int get_lines_of_file(char *path)
{
    assert(path != NULL);

    int line = 0;
    char buf[LINE_SIZE];
    FILE *fd = fopen(path, "r");
    if (fd < 0)
    {
        fprintf(stderr, "can't open %s!\n", path);
        return -1;
    }

    memset(buf, 0, LINE_SIZE);
    while(feof(fd) == 0)
    {
        if(fgets(buf, LINE_SIZE, fd) != NULL)
        {
            line++;
        }
    }

    fclose(fd);

    return line;
}

int main(void)
{
    printf("%d\n",get_lines_of_file("/home/Bran/Public/orangesrc/libc/file_system.c"));
    return 0;
}


